﻿using DropBoxClient.Entities;
using DropBoxClient.Framework;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

namespace DropBoxClient.Operations
{
    internal class SearchOperation
    {
        private static readonly string Url = "https://api.dropbox.com/1/search/{0}/{1}";
        private static readonly string MethodType = StringConstants.PostMethodType;

        private WebOperation<List<Meta>> _operation;
        private string _accessToken;

        internal string Root { get; set; }
        internal string Path { get; set; }
        internal string Query { get; set; }

        internal SearchOperation(string accessToken)
        {
            _accessToken = accessToken;
        }

        internal List<Meta> Execute()
        {
            if (Query.Length < 3)
                throw new InvalidDataException("Query should have 3 characters atleast");
            var url = string.Format(Url, Root, Path);
            var parameterCollection = new NameValueCollection();
            parameterCollection.Add("query", Query);
            _operation = new WebOperation<List<Meta>>(url, _accessToken, MethodType, parameterCollection);
            return _operation.Execute();
        }
    }
}
